<?php
/**
 * Class DB
 */
class DB {
    private static $_instance = null;	
    private $_user = "root";
    private $_pass = "gamestart";
    private $_dbName = "xiang";
    private $_dbHost = "localhost";
    private $_conn = null;

    private function __construct() {
        $this->_conn = mysql_connect($this->_dbHost, $this->_user, $this->_pass)
                or die("Could not connect to db: " . mysql_error());
        mysql_query("SET NAMES 'utf8'");
        mysql_select_db($this->_dbName, $this->_conn)
                or die("Could not select db: " . mysql_error());
    }

    /**
     * instance a database connetcion
     * @return DB 
     */
    public static function getInstance() {
        if (!self::$_instance instanceof self) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }
    
    /**
     * execute query,update,insert,delete,drop, etc
     * @param String $sql
     * @return boolean|multitype
     */
    static public function query($sql) {
        if($result = mysql_query($sql, self::getInstance()->_conn)){
			return $result;
		} else {
			echo $sql."<br /> error: ".mysql_error();
		}
    }

    /**
     * get the row nums 
     * @param String $sql
     * @return number:int
     */
    static public function getRowNum($sql) {
    	 $result = self::query($sql);
    	 return mysql_num_rows($result);
    }
    
    /**
     * execute query, return one record
     * @param String $sql
     * @param const $type
     * @return false| array
     */
    static public function getRow($sql, $type = MYSQL_ASSOC) {
        $result = self::query($sql);
        if($result == FALSE) {
        	return FALSE;
        } 
        return mysql_fetch_array($result, $type);
    }

    /**
     * execute query, return multi records
     * @param String $sql
     * @param const $type
     * @return false|array
     */
    static public function getRows($sql, $type = MYSQL_ASSOC) {
        $result = self::query($sql);
        if($result == FALSE) {
        	return FALSE;
        } 
        while ($row = mysql_fetch_array($result, $type)) {
            $rows[] = $row;
        }
        return $rows;
    }

    /**
     * start transaction
     */
    static public function startTransaction() {
    	self::query('START TRANSACTION');  
    }
    
    /**
     * rollback 
     */
    static public function rollback() {
    	self::query("ROLLBACK");
    }
    
	/**
     * commit transaction
     */
    static public function commitTransaction() {
    	self::query('COMMIT');  
    }
    
	/**
	 * close the connection
	 * @return boolean
	 */
	static public function close() {  
		return mysql_close(self::getInstance()->_conn);  
	} 
	
	/**
	 * correct mysql time zone
	 */
	static public function setTimeZone() {
		$sql = 'SET time_zone = "+8:00"';
		self::query($sql) or die("set time zone fails");  
	}
	
	/**
	 * 返回下一个插入的主键ID
	 */
	static public function newInsertId()
	{
		return mysql_insert_id(self::getInstance()->_conn);
	}
}
?>
